package com.example.blogdemo.service;

import org.apache.commons.lang3.CharUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2017-12-18
 **/
@Service
public class SensitiveService implements InitializingBean{

    public class TrieNode{
        private boolean end =false;
        private Map<Character,TrieNode> subNodes = new HashMap<>();
        public void addSubNode(Character c,TrieNode node){
            subNodes.put(c,node);
        }
        public TrieNode getSubNode(Character c){
            return subNodes.get(c);
        }
        public boolean isEnd(){
            return end;
        }
        public void setNodeEnd(boolean end){
            this.end = end;
        }
    }

    private TrieNode root = new TrieNode();

    @Override
    public void afterPropertiesSet() throws Exception {
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("SensitiveWords.txt");
        InputStreamReader reader = new InputStreamReader(is);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String lineTxt;
        while((lineTxt=bufferedReader.readLine())!=null){
            addword(lineTxt.trim());
        }
        reader.close();
        is.close();
    }
    public boolean isSymbol(Character c){
        int ic = (int)c;
        return !CharUtils.isAsciiAlphanumeric(c)&&(ic<0x2E80 || ic>0x9FFF);
    }

    //外面有套了一层root而已，不影响
    private void addword(String lineTxt){
        TrieNode currentNode = root;
        for(int i =0;i<lineTxt.length();i++){
            Character c = lineTxt.charAt(i);
            if(isSymbol(c))
                continue;
            TrieNode node = currentNode.getSubNode(c);
            if(node==null){
                node = new TrieNode();
                currentNode.addSubNode(c,node);
            }
            currentNode = node;
            if(i==lineTxt.length()-1)
                currentNode.setNodeEnd(true);
        }
    }

    public String filter(String text){
        StringBuilder result = new StringBuilder();
        String replacment = "***";
        TrieNode tmpNode =root;
        int begin = 0;
        int position = 0;
        while(position<text.length()){
            Character c = text.charAt(position);
            if(isSymbol(c)){
                if(tmpNode==root){
                    result.append(c);
                    begin++;
                }
                position++;
                continue;
            }
            tmpNode = tmpNode.getSubNode(c);
            if(tmpNode == null){
                if(position!=begin){
                    result.append(text.substring(begin,position));
                    begin=position;
                    tmpNode=root;
                }else{
                    result.append(c);
                    position++;
                    begin=position;
                    tmpNode=root;
                }
            }else if(tmpNode.isEnd()){
                result.append(replacment);
                position++;
                begin=position;
                tmpNode=root;
            }else{
                position++;
            }
        }
        return result.toString();
    }




}
